let itemCount = 0
let currentPage = 1

// let Id = window.location.search.split('=')[1]
// console.log(Id)
//
let id = window.location.search.split('=')[1]
console.log(id)
const zuopin_1 = document.querySelector('.zuopin_1')

// 创建表单参数对象
//作品
const params1 = new URLSearchParams();
params1.append('authorId', id);  // 添加参数（键值对形式）
//在读
// pageNum: 1
// pageSize: 10
// total: 3
const params2 = new URLSearchParams();
params2.append('userId', id);  // 添加参数（键值对形式）
//
//作者作品
const p1 = document.querySelector('.p1')
const p2 = document.querySelector('.p2')
const booklists_ul = document.querySelector('.booklists-ul')
const laypage = document.getElementById('demo-laypage-all')

const zuopin = ()=>{
    axios.post('https://homeapi.zongheng.com/api/author/book',{
        authorId: 166130
    }
    
    ).then(res=>{
        console.log(res.data)
        booklists_ul.innerHTML=''
        zuopin_1.innerHTML  = `
        <p>
            <span><i>本站作品</i><span>${res.data.data.length}</span></span>
            <em></em>
            <span><i>累计字数</i><span>2931.1W</span></span>
            <em></em>
            <span><i>创作天数</i><span>4066</span></span>
            <em></em>
        </p>
        `
        res.data.data.forEach(item=>{
            
            booklists_ul.innerHTML +=`
            <div class="li">
                <img class="li_img" src="${item.coverUrl}"  onclick="ju()">
                <div class="li_mid">
                    <h4 onclick="ju()">${item.bookName}</h4>
                    <div class="status">
                        <div class="over">连载中</div>
                        <em class="sp"></em>
                        <div class="cat">${item.catePname}</div>
                        <em class="dot"></em>
                        <div class="tnum">6万字</div>
                    </div>
                    <div class="intro">
                        ${item.description}
                    </div>
                    <div class="update">
                        <span class="ttit">最新章节：</span>
                        <a>${item.updateChapterName}</a>
                        <div class="uptime">${item.chapterUpdateTime}</div>
                    </div>
                </div>
                <div class="li_right">
                    <a>立即阅读</a>
                </div>
            </div>
            `
        })
    }).catch(err=>{
        console.log(err)
    })
}
//在读
const zaidu = ()=>{ 
    axios.post('https://homeapi.zongheng.com/api/show/bookShelf',{
        userId: 166130
    }
    ).then(res=>{
        console.log(res.data,2)
        booklists_ul.innerHTML=''
        itemCount = res.data.data.total
        console.log(itemCount)  
        res.data.data.data.forEach(item=>{
            
            booklists_ul.innerHTML +=`
            <div class="li">
                <img class="li_img" src="https://static.zongheng.com/upload/${item.coverUrl}"  onclick="ju()">
                <div class="li_mid">
                    <h4  onclick="ju()">${item.bookName}</h4>
                    <div class="status">
                        <div class="over">连载中</div>
                        <em class="sp"></em>
                        <div class="cat">${item.catePname}</div>
                        <em class="dot"></em>
                        <div class="tnum">6万字</div>
                    </div>
                    <div class="intro">
                        ${item.description}
                    </div>
                    <div class="update">
                        <span class="ttit">最新章节：</span>
                        <a>${item.updateChapterName}</a>
                        <div class="uptime">${item.chapterUpdateTime}</div>
                    </div>
                </div>
                <div class="li_right">
                    <a>立即阅读</a>
                </div>
            </div>
            `
        })
        pagination()
    }).catch(err=>{
        console.log(err)
    })
}
p1.onclick = ()=>{
    zuopin()
    booklists_ul.innerHTML=''
    p2.classList.remove('p_active')
    p1.classList.add('p_active')  
    laypage.style.display = 'none';
}
zuopin()
p2.onclick = ()=>{
    booklists_ul.innerHTML=''
    currentPage = 1;
    params2.set('pageNum', 1);
    params2.set('pageSize', 10);
    zaidu() 
    p1.classList.remove('p_active')
    p2.classList.add('p_active')
    laypage.style.display = 'block';
}




const pagination = ()=>{
    layui.use(function(){
        var laypage = layui.laypage;
        // 完整显示
        laypage.render({
        elem: 'demo-laypage-all', // 元素 id
        limit: 10,
        count: itemCount, // 数据总数
        layout: ['prev', 'page', 'next'], // 功能布局
        curr: currentPage, // 初始获取 hash 值为 curr 的当前页
        hash: 'curr' ,// 自定义 hash 名称
        jump: function(obj, first){
            // console.log(obj.curr); // 得到当前页，以便向服务端请求对应页的数据。
            // console.log(obj.limit); // 得到每页显示的条数
            // 首次不执行
            if(!first){
            // do something
            currentPage = obj.curr
            params2.set('pageNum', obj.curr);
            params2.set('pageSize', obj.limit);
            zaidu()
            }
        }
        });
    });
}

jump=()=>{
    window.location.href='index.html'
}
jump1=()=>{
    window.location.href='bookstock.html'
}
jump2=()=>{
    window.location.href='naodongxingqiu_paihang.html'
}
to=()=>{
    console.log(9999)
     const inputElement = document.querySelector('.search input');
    const searchText = inputElement.value.trim();
        console.log(searchText);
        inputElement.value = '';
        window.location.href = 'shearh.html?searchText=' + searchText;
}
ju=()=>{
    console.log(9999)
    window.location.href='detail.html'
}
